<?php

namespace App\Http\Controllers;

use App\Models\ImgModel;
use Illuminate\Http\Request;

class ToolsController extends Controller
{
    public function uploadImage(Request $request) {
        $file = $request->file('file');
        // 使用文件的hash生成文件名
        $fileName = md5_file($file->getRealPath()) . '.' . $file->getClientOriginalExtension();
        
        $path = 'uploads/' . $fileName;
        $url = url($path);
        $absPath = public_path($path);
        // 文件已经存在
        if (file_exists($absPath)) {
            return self::sendOk($url);
        }
        $file->move(public_path('uploads'), $fileName);
        ImgModel::create([
            'path' => $url,
            'created_at' => now(),
        ]);

        return self::sendOk($url);
    }
}
